﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair.Report
{
    public partial class frmfakatongji : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmfakatongji()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(constr);
            SqlConnection conn1 = new SqlConnection(constr);
            conn.Open();
            conn1.Open();
            //查询卡分类
            string sql = "SELECT [cid] ,[cname] FROM [BHairDB].[dbo].[card]";
            SqlCommand comm = new SqlCommand(sql,conn);
            SqlDataReader sdr = comm.ExecuteReader();
            string card;
            int cs;
            var dt = new DataTable();
            dt.Columns.Add(new DataColumn("卡分类", typeof(string)));
            dt.Columns.Add(new DataColumn("张数", typeof(int)));
            while (sdr.Read())
            {
                card = sdr["cname"].ToString();
                string sql1 =string.Format( "SELECT COUNT(*) FROM member  JOIN card on card.cid=member.mcid where member.mjoin>='{0}' and member.mjoin<='{1}' and card.cname='{2}'",dateTimePicker1.Text,dateTimePicker2.Text,card);
                SqlCommand comm1 = new SqlCommand(sql1, conn1);
                if (comm1.ExecuteScalar().ToString() != string.Empty)
                {
                    cs = Int32.Parse(comm1.ExecuteScalar().ToString());
                }
                else
                {
                    cs = 0;
                }
                var row = dt.NewRow();
                row["卡分类"] = card;
                row["张数"] = cs;
                dt.Rows.InsertAt(row, 0);

            }
            dataGridView1.DataSource = dt;
            fkmx();
            conn.Close();
            conn1.Close();

        }
        public void fkmx()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("SELECT [mid] as 卡号,[mname] as 持卡人,card.cname as 卡类别,[mremark] as 备注 ,case([msex]) when '0' then '女' when '1' then '男' else '其他' end as 性别 ,[mphone] as 电话 ,[maddress] as 地址 ,[mother] as 其他信息 ,[mjoin] as 办卡日期 ,[midcard] as 身份证号 ,[mbalance] as 余额 ,[mpoint] as 积分,[yxq] as 有效期 ,[shengao] as 身高 ,[tizhong] as 体重 ,[xiongwei] as 胸围 ,[yaowei] as 腰围 ,[tunwei] as 臀围 FROM [BHairDB].[dbo].[member] join card on card.cid=member.mcid where mjoin>='{0}' and mjoin<='{1}'",dateTimePicker1.Text,dateTimePicker2.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView2.DataSource = dt;
        }

        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
       e.RowBounds.Location.Y,
       dataGridView1.RowHeadersWidth - 4,
       e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
            dataGridView1.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
    }

    }


